//index.js
//获取应用实例
var app = getApp()
var QQMapWX = require('../../../resource/js/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		action:'',
		account:app.setting.account,
		ShowAuthM:false,
		pagetitle:'下单页面',
		pcd: ['广东省', '广州市', '海珠区'],
	},
	onReady: function (){
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (){
		var page=this;
		var ShowAuthM=page.data.ShowAuthM;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		app.util.get(app.access,function(pagedata){
			console.log('create');console.log(pagedata);
			if((!pagedata.member.nickname || !pagedata.member.avatar || pagedata.member.nickname=='匿名用户') && !app.isgetUserInfo){
					ShowAuthM=!ShowAuthM
			}
			pagedata.post.endprice=app.util.price(pagedata.post.endprice);
			pagedata.post.no_dispatch_price=app.util.price(pagedata.post.no_dispatch_price);
			pagedata.post.dispatchprice=app.util.price(pagedata.post.dispatchprice);
			pagedata.post.olddispatchprice=app.util.price(pagedata.post.olddispatchprice);
			pagedata.post.diyformfields=pagedata.post.diyfields;
			page.setData({
				pagedata:pagedata,
				ShowAuthM:ShowAuthM,
				nodatapic_close:true,
			});
			page.refresh(pagedata.merchs);
			page.get_tmpids();
		});
	},
	get_tmpids: function (){
		var the=this
		app.util.get('jy_weishop/get_data/get_tmpids/',function(res){
			//console.log('get_tmpids');console.log(res);
			the.setData({
				'pagedata.tmplIds':res.list,
			});
		});
	},
	showdelivery:function(){
		var page=this
		var showdelivery = page.data.showdelivery
		page.setData({
			showdelivery:!showdelivery
		});
	},
	showfapiao:function(){
		var page=this
		var showfapiao = page.data.showfapiao
		page.setData({
			showfapiao:!showfapiao
		});
	},
	showcoupon:function(e){
		var the=this
		var data = e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var showcoupon = page.data.showcoupon
		pagedata.coupons=pagedata.merchs[data.index].coupons;
		pagedata.nowmerchkey=data.index;
		the.setData({
			'pagedata.coupons':pagedata.coupons,
			'pagedata.nowmerchkey':pagedata.nowmerchkey,
			showcoupon:!showcoupon
		});
	},
	setaddress: function (e) {
		var the=this
		var data = e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		app.util.get('core/member.address/set/id='+data.id+'&get=1',function(res){
			for(var i in pagedata.address_list){
				pagedata.address_list[i].isdefault=0
			}
			pagedata.address_list[data.index].isdefault=1
			pagedata.post.addressid=data.id
			the.setData({
				'pagedata.address_list':pagedata.address_list,
				'pagedata.post':pagedata.post,
				modal_address:false,
				'pagedata.post.address':pagedata.address_list[data.index]
			});
			app.util.msg('设置成功');
		});
	},
	edit_address:function(e){
		var the =this
		var data = e.currentTarget.dataset;
		var pagedata = the.data.pagedata
		the.setData({
			modal_address:false,
			edit_address:true,
			'pagedata.isaddaddress':data.isadd?1:0
		})
		var pcd =the.data.pcd
		if(!data.id){
			//获取区域城市
			qqmapsdk = new QQMapWX({
				key: pagedata.qqmap_ak?pagedata.qqmap_ak:'SN2BZ-MKT32-22SU7-COVX6-ZYXAQ-OEF6D'
			});
			app.util.location(function(res){
				qqmapsdk.reverseGeocoder({
						location:{latitude:res.latitude,longitude:res.longitude},
						success:function(res){
							if(res.result.address_component.province){
								 pcd[0] = res.result.address_component.province
							}
							if(res.result.address_component.city){
								pcd[1] = res.result.address_component.city
							}
							if(res.result.address_component.district){
								pcd[2] = res.result.address_component.district
							}
							the.setData({
								pcd:pcd,
							})
						}
				});
			});
		}else{
			var oldpcd = pagedata.address_list[data.index].pcd
			pcd[0] = oldpcd.province
			pcd[1] = oldpcd.city
			pcd[2] = oldpcd.district
			console.log(pcd);
		}
		pagedata.address_item=data.id?pagedata.address_list[data.index]:{};
		the.setData({
			'pagedata.address_item':pagedata.address_item,
			pcd:pcd,
		});
	},
	close_address:function(){
		var the =this
		the.setData({
			edit_address:false,
			modal_address:false
		})
	},
	del_address:function(e){
		var the =this
		var data = e.currentTarget.dataset;
		var pagedata = the.data.pagedata
		app.util.get('core/member.address/del/id='+data.id+'&get=1',function(res){
			pagedata.address_list.splice(data.index,1);
			the.setData({
				'pagedata.address_list':pagedata.address_list
			});
			app.util.msg('删除成功');
		});
	},
	addressRegionChange:function (e){
		var the = this
		console.log('addressRegionChange');console.log(e);
		var pcd='';
		if(e.pcd){
			var pcd = e.pcd
		}else if(e.detail){
			var pcd = e.detail.pcd
		}
		if(pcd){
			the.setData({
			  'region': pcd,
			  'pcd':pcd,
			})
		}
    },
	post_address:function(e){
		var the =this
		var data=e.detail.value;
		var pagedata=the.data.pagedata
		data.pcd={}
		data.pcd.province=the.data.pcd[0]
		data.pcd.city=the.data.pcd[1]
		data.pcd.district=the.data.pcd[2]
		data.id=pagedata.address_item.id?pagedata.address_item.id:'';
		data.get=1
		app.util.post('core/member.address/post/',data,function(res){
			if(res){
				pagedata.post.addressid=data.id?data.id:res;
				if(!data.id){
					pagedata.address_list=pagedata.address_list.concat(data)
				}
				the.setData({
					'pagedata.address_list':pagedata.address_list,
					'pagedata.post':pagedata.post,
					'pagedata.post.address':data
				});
				app.util.msg('保存成功');
				app.util.get('jy_weishop/order.create/get_freight/showbaoyou=1&addressid='+pagedata.post.addressid,function(freight){
					the.up_freightfee(freight)
				});
			}else{
				app.util.msg('保存失败');	
			}
			the.close_address();
		});
	},
	up_freightfee:function(freight){
		var the=this
		var pagedata = the.data.pagedata
		for(var i=0;i<pagedata.merchs.length;i++){
			pagedata.merchs[i].post.endprice=parseFloat(pagedata.merchs[i].no_dispatch_price)+parseFloat(freight.fee);
			pagedata.merchs[i].post.dispatchprice=freight.fee;
			pagedata.merchs[i].post.addbaoyoufee=freight.addbaoyoufee;
			pagedata.merchs[i].post.isonebaoyou=freight.isonebaoyou;
			pagedata.merchs[i].post.is_peisong=freight.is_peisong?freight.is_peisong:0;
			if(pagedata.merchs[i].sendtypes[1]){
				pagedata.merchs[i].sendtypes[1]=scope.pagedata.main.is_peisong==1?'同城配送':'快递配送';
			}
			pagedata.merchs[i].post.outarea=freight.outarea;
			pagedata.merchs[i].post.no_out_price=freight.no_out_price?freight.no_out_price:0;
			pagedata.merchs[i].post.baoyou_man=freight.baoyou_man?freight.baoyou_man:0;
			pagedata.merchs[i].post.peisong_juli=freight.juli?freight.juli:pagedata.merchs[i].post.peisong_juli;
		}
		the.setData({
			'pagedata.merchs':pagedata.merchs
		})
		the.refresh(pagedata.merchs);
	},
	modal_address:function(){
		var page=this
		var pagedata = page.data.pagedata
		var modal_address = page.data.modal_address
		app.util.get('core/member.address/',function(res){
			page.setData({
				'pagedata.address_list':res.list
			})
		})
		page.setData({
			modal_address:!modal_address,
			edit_address:false,
		});
	},
	showremark:function(){
		var page=this
		var showremark = page.data.showremark
		page.setData({
				showremark:!showremark
		});
	},
	//刷新总数和总价
	refresh:function(merchs){
		var the = this
		var pagedata = the.data.pagedata
		var total=0,endprice=0.00;
		var no_dispatch_price=0;
		pagedata.showbuy=false;
		for(var i=0;i<merchs.length;i++){
		   var merch=merchs[i];
			total+=parseInt(merch.total);
			endprice+=merch.post.endprice;
			no_dispatch_price+=merch.post.no_dispatch_price;
			if(merch.no_out_price>0 || merch.outarea==1){
			}else{
				pagedata.showbuy=true;
			}
		}
		console.log(pagedata.post);
		pagedata.post.total=total;
		pagedata.post.endprice=app.util.price(endprice);
		pagedata.post.no_dispatch_price=app.util.price(no_dispatch_price);
		the.setData({
				'pagedata.post':pagedata.post,
				'pagedata.showbuy':pagedata.showbuy
		});
	},
	post:function(e){
		var the = this
		var pagedata = the.data.pagedata
		pagedata.post.peerpay=true
		app.util.post('jy_weishop/order.createall/post/',pagedata.post)
	},
	showgiftpicker:function(){
		var page=this
		var showgiftpicker = page.data.showgiftpicker
		page.setData({
				showgiftpicker:!showgiftpicker
		});
	},
	giveUp:function(){
		var page=this
		var giveUp = page.data.giveUp
		if(giveUp){
			this.giveUpMD.hide();
		}else{
			this.giveUpMD.show();
		}
		page.setData({
				giveUp:!giveUp
		});
	},
	change_address:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var value = e.currentTarget.dataset
		pagedata.post.address = value.item
		pagedata.post.addressid=value.item.id;
		app.util.get('jy_weishop/order.create/get_freight/showbaoyou=1&addressid='+pagedata.post.address.id,function(freight){
			pagedata.post.endprice=parseFloat(pagedata.post.endprice)-parseFloat(pagedata.order.dispatchprice)+parseFloat(freight.fee);
			pagedata.post.endprice=pagedata.post.endprice>0?app.util.price(pagedata.post.endprice):0;
			pagedata.order.dispatchprice=freight.fee;
			pagedata.post.dispatchprice=freight.fee;
			pagedata.order.addbaoyoufee=freight.addbaoyoufee;
			pagedata.order.isonebaoyou=freight.isonebaoyou;
			pagedata.order.outarea=freight.outarea;
			pagedata.order.no_out_price=freight.no_out_price?freight.no_out_price:0;
			pagedata.order.baoyou_man=freight.baoyou_man?freight.baoyou_man:0;
			pagedata.order.peisong_juli=freight.juli?freight.juli:pagedata.order.peisong_juli;
			the.setData({
				'pagedata.order':pagedata.order,
				'pagedata.post':pagedata.post,
				'pagedata.item':pagedata.item,
			})
		});
		the.close_address();
	},
	sendtype:function(e){
		var page=this
		var data = e.currentTarget.dataset;
		var pagedata= page.data.pagedata
		pagedata.post.sendtype=data.type;
		page.setData({
				'pagedata.post':pagedata.post
		});
	},
	make_type:function(e){
		var page=this
		var data = e.currentTarget.dataset;
		var pagedata= page.data.pagedata
		pagedata.post.make_type=data.type;
		page.setData({
				'pagedata.post':pagedata.post
		});
	},
	invoicetype:function(e){
		var page=this
		var data = e.currentTarget.dataset;
		var pagedata= page.data.pagedata
		pagedata.post.invoicetype=data.type;
		page.setData({
				'pagedata.post':pagedata.post
		});
	},
	fapiaotype2:function(e){
		var page=this
		var data = e.currentTarget.dataset;
		var pagedata= page.data.pagedata
		pagedata.post.fapiaotype2=data.type;
		pagedata.post.invoicename=pagedata.post.address.name;
		page.setData({
				'pagedata.post':pagedata.post
		});
	},
	invoicegoodstype:function(e){
		var page=this
		var data = e.currentTarget.dataset;
		var pagedata= page.data.pagedata
		pagedata.post.invoicegoodstype=data.type;
		page.setData({
				'pagedata.post':pagedata.post
		});
	},
	setrealname:function(e){
		var the =this
		var value=e.detail.value;
		var pagedata= the.data.pagedata
		the.setData({
				'pagedata.post.realname':value,
		});
	},
	setmobile:function(e){
		var the =this
		var value=e.detail.value;
		var pagedata= the.data.pagedata
		the.setData({
				'pagedata.post.mobile':value,
		});
	},
	upremark:function(e){
		var the =this
		var data=e.detail.value;
		var pagedata= the.data.pagedata
		pagedata.post.remark=data.remark;
		the.setData({
				'pagedata.post':pagedata.post,
				showremark:false
		});
	},
	showstores:function(){
		var the =this
		the.setData({
				carrier_list:true
		});
	},
	checkcoupon:function(e){
		var the =this
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var nowmerchkey=pagedata.nowmerchkey
	    var nowmerch=pagedata.merchs[nowmerchkey];
		if(data.type==1){
			if(nowmerch.post.backtype!=1){
				nowmerch.post.no_dispatch_price = parseFloat(nowmerch.no_dispatch_price) - nowmerch.post.deduct
				nowmerch.post.endprice = parseFloat(nowmerch.endprice) - nowmerch.post.deduct
			}else{
				nowmerch.post.no_dispatch_price = parseFloat(nowmerch.no_dispatch_price) * nowmerch.post.discount / 10;
				nowmerch.post.endprice = nowmerch.endprice * nowmerch.post.discount/10		
			}
		}else{
			nowmerch.post.couponid=0;
			if(nowmerch.post.backtype!=1){
				nowmerch.post.no_dispatch_price = nowmerch.no_dispatch_price
				nowmerch.post.endprice = nowmerch.endprice
			}else{
				nowmerch.post.no_dispatch_price = nowmerch.no_dispatch_price;
				nowmerch.post.endprice = nowmerch.endprice		
			}
		}
		nowmerch.post.endprice=nowmerch.post.endprice>0?app.util.price(nowmerch.post.endprice):0;
		nowmerch.post.no_dispatch_price=app.util.price(nowmerch.post.no_dispatch_price);
		pagedata.merchs[nowmerchkey]=nowmerch;
	    the.refresh(pagedata.merchs);
		the.setData({
			'pagedata.merchs':pagedata.merchs,
		});
		the.showcoupon();
	},
	selectcoupon:function(e){
		var the =this
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var nowmerchkey=pagedata.nowmerchkey
	    var nowmerch=pagedata.merchs[nowmerchkey];
		if(data.type){
			nowmerch.post.couponid=pagedata.wxcards[data.index].id
			nowmerch.post.backtype=pagedata.wxcards[data.index].coupon.backtype;
			nowmerch.post.discount=pagedata.wxcards[data.index].discount;
			nowmerch.post.deduct=pagedata.wxcards[data.index].deduct;
			nowmerch.post.title5=pagedata.wxcards[data.index].coupon.title5
		}else{
			nowmerch.post.couponid=nowmerch.coupons[data.index].id
			nowmerch.post.backtype=nowmerch.coupons[data.index].coupon.backtype;
			nowmerch.post.discount=nowmerch.coupons[data.index].discount;
			nowmerch.post.deduct=nowmerch.coupons[data.index].deduct;
			nowmerch.post.title5=nowmerch.coupons[data.index].coupon.title5
		}
		pagedata.merchs[nowmerchkey]=nowmerch;
		the.setData({
				'pagedata.merchs':pagedata.merchs,
		});
	},
	selectstore:function(e){
		var the=this
		var pagedata=the.data.pagedata
		var data=e.currentTarget.dataset;
		pagedata.selectstore=pagedata.carrier_list[data.index]
		pagedata.post.storeid = pagedata.carrier_list[data.index].id;
		the.setData({
				'pagedata.selectstore':pagedata.selectstore,
				'pagedata.post':pagedata.post,
				carrier_list:false
		});
	},
	dayid:function(e){
		var data=e.currentTarget.dataset;
		var the=this;
		var pagedata=the.data.pagedata;
		pagedata.post.dayid=data.index
		the.setData({
			'pagedata.post':pagedata.post
		})
	},
	out_time:function(e){
		var the=this
		var pagedata=the.data.pagedata
		var data=e.currentTarget.dataset;
		pagedata.post.out_time=data.time
		the.setData({
				'pagedata.post':pagedata.post,
				selecttime:false
		});
	},
	closetime:function(){
		var the=this;
		the.setData({
				selecttime:false
		})
	},
	selecttime:function(){
		var the=this;
		var pagedata=the.data.pagedata;
		var store_id=pagedata.post.id
		app.util.get('jy_weishop/store.index/out_time/store_id='+store_id,function(res){
			the.setData({
				'pagedata.out_time':res.out_time,
				'pagedata.predict_time':res.predict_time,
				'pagedata.timestamp':res.timestamp,
				selecttime:true
			})
		});
	},
	openAddress: function () {
		var the=this
		var pagedata=the.data.pagedata
		var pcd=[];
		wx.chooseAddress({
			success (res) {
				pagedata.address_item={}
				pagedata.address_item.name = res.userName; // 收货人姓名
				pcd[0] = res.provinceName; // 国标收货地址第一级地址（省）
				pcd[1] = res.cityName; // 国标收货地址第二级地址（市）
				pcd[2] = res.countyName; // 国标收货地址第三级地址（国家）
				pagedata.address_item.address = res.detailInfo; // 详细收货地址信息
				pagedata.address_item.mobile = res.telNumber; // 收货人手机号码
				the.setData({
				  'pagedata.address_item': pagedata.address_item,
				  'pcd':pcd
				})
			},fail (res) {
				console.log('fail');console.log(res)
			}
		})
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	//订阅消息
	SubscribeMessage: function (orderid) {
		var the=this
		var pagedata=the.data.pagedata
		the.setData({
				ShowSubMsg:false,
		});
		wx.requestSubscribeMessage({
			'tmplIds':pagedata.tmplIds,
			success:function (res) {
				console.log('SubscribeMessage success');console.log(res);
				if(res[pagedata.tmplIds[0]]=='accept' && orderid){
					app.util.get('jy_weishop/order.list/sendsubmsg/id='+orderid,function(res){
						app.util.msg('下单成功！','','jy_weishop/order.list/');
					});
				}
			},
			fail:function (res) {
				console.log('SubscribeMessage fail468');console.log(res);
				app.util.msg('下单成功！','','jy_weishop/order.list/');
			},
		})
	},
	//唤醒订阅授权
	sendsubmsg: function (orderid) {
			var page=this
			var pagedata=page.data.pagedata
			if(pagedata.tmplIds && pagedata.tmplIds[0]){
				console.log('sendsubmsg');
				app.util.getsubmsgSet(pagedata.tmplIds,function(ShowSubMsg){
					if(ShowSubMsg){
						page.SubscribeMessage(orderid);
					}else{
						console.log('getsubmsgSet fail：'+ShowSubMsg);
						app.util.msg('下单成功！','','jy_weishop/order.list/');
					}
				});
			}else{
				app.util.msg('下单成功！','','jy_weishop/order.list/');
			}
	},
	closepaymodal:function(){
		var page=this
		page.setData({
				showpaymodal:false
		});
	},
	pay_credit:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_credit(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open)
	},
	pay_wechat:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_wechat(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
					//app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open);
	},
	pay_alipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_alipay(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open);
	},
    pay_ttpay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_ttpay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open)
	},
	pay_hdfk:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_hdfk(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
					//app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		})
	},
	pay_apipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_apipay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open,pagedata.paymodal.api_name)
	},
	radiotype:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		the.setData({
			'pagedata.paymodal.paytype':data.type,
			'pagedata.paymodal.api_name':data.name
		})
	},
	radiodi_open:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var di_open=pagedata.paymodal.di_open;
		di_open=di_open==data.type?'':data.type;
		if(!di_open){
			data.difee=0;
		}
		the.setData({
			'pagedata.paymodal.di_open':di_open,
			'pagedata.paymodal.di_fee':data.difee
		})
	},
	new_gotopay:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var paytype=pagedata.paymodal.paytype;
		if(paytype=='credit'){
			the.pay_credit();
		}else if(paytype=='wechat'){
			the.pay_wechat();
		}else if(paytype=='alipay'){
			the.pay_alipay();
		}else if(paytype=='hdfk'){
			the.pay_hdfk();
		}else if(paytype=='apipay'){
			the.pay_apipay();
		}else if(paytype=='ttpay'){
			the.pay_ttpay();
		}else{
			app.util.msg('请选择支付方式！');
		}
	},
	peerpay:function(){
		var page=this
		var pagedata=page.data.pagedata
		pagedata.post.peerpay=true;
		app.util.post('jy_weishop/order.create/post/',pagedata.post);
	},
	diyform_input:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var value = e.detail.value
		var data = e.currentTarget.dataset;
		console.log(value)
		if(pagedata.post && pagedata.post.diyformfields && pagedata.post.diyformfields[data.childindex]){
			pagedata.post.diyformfields[data.childindex].data = value
			pagedata.post.diyformfields[data.childindex].selectkey = data.index
			//console.log(pagedata.post.diyformfields)
			the.setData({
				'pagedata.post.diyformfields':pagedata.post.diyformfields
			})
		}
    },
	diyform_duoxuan:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset;
		var value = e.detail.value
		console.log(value)
		if(pagedata.post && pagedata.post.diyformfields && pagedata.post.diyformfields[data.childindex]){
			pagedata.post.diyformfields[data.childindex].data = value
			the.setData({
				'pagedata.post.diyformfields':pagedata.post.diyformfields
			})
		}
    },
	bindRegionChange:function(e) {
		var the = this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset
		var region = e.detail.value
		pagedata.post.diyformfields[data.childindex].data.province = region[0]
		pagedata.post.diyformfields[data.childindex].data.city = region[1]
		pagedata.post.diyformfields[data.childindex].data.district = region[2]
		the.setData({
		  region: region,
		  'pagedata.post.diyformfields':pagedata.post.diyformfields
		})
    },
	no_deduct_credit:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		pagedata.post.no_deduct_credit=pagedata.post.no_deduct_credit==1?0:1;
		if(pagedata.post.no_deduct_credit==1){
			pagedata.post.no_dispatch_price = pagedata.post.no_dispatch_price+parseFloat(pagedata.order.deduct_credit)
			pagedata.post.endprice = pagedata.post.endprice+parseFloat(pagedata.order.deduct_credit)
		}else{
			pagedata.post.no_dispatch_price = pagedata.post.no_dispatch_price-parseFloat(pagedata.order.deduct_credit)
			pagedata.post.endprice = pagedata.post.endprice-parseFloat(pagedata.order.deduct_credit)
		}
		the.setData({
			'pagedata.post':pagedata.post,
		})
	},
	no_di_cardfee:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var merch=pagedata.merchs[data.indx];
		merch.post.no_di_cardfee=merch.post.no_di_cardfee==1?0:1;
		if(merch.post.no_di_cardfee==1){
			merch.post.no_dispatch_price = merch.no_dicard_dispatch_endprice
			merch.post.endprice = merch.no_dicard_endprice
		}else{
			merch.post.no_dispatch_price -= parseFloat(merch.no_dispatch_cardfee)
			merch.post.endprice -= parseFloat(merch.di_cardfee)
			if(merch.post.endprice<0){
				merch.post.endprice=0
			}
			if(merch.post.no_dispatch_price<0){
				merch.post.no_dispatch_price=0
			}
		}
		pagedata.merchs[data.indx]=merch;
		the.setData({
			'pagedata.merchs':pagedata.merchs,
		})
	},
	up_file:function(e){
		var the =this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset
		app.util.up_file('image',function(res){
			console.log(res)
			if(data.source_type=='images'){
				pagedata.post.diyformfields[data.childindex].data.push(res);
			}else{
				pagedata.post.diyformfields[data.childindex].data = res
			}
			the.setData({
				'pagedata.post.diyformfields':pagedata.post.diyformfields
			})	
		});
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		var the=this;
		app.util.getUserInfo(e,function(res){
			if(res && res.userInfo){
				app.isgetUserInfo=true
				the.setData({
					ShowAuthM:false
				})
				the.onLoad();
			}else{
				return false;
			}
		});
	},
	closeAuthM:function(){
		var the=this;
		the.setData({
			ShowAuthM:false
		})
	},
	formSubmit:function(){
		var page=this
		var pagedata=page.data.pagedata
		if(pagedata.order_id){
			app.util.msg('你已下单！','','jy_weishop/order.list/');
			return;
		}
		var str = /^[1][3,4,5,7,8][0-9]{9}$///手机
		var strname =  /^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$/ //中文
		var stremail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
		var filedata = pagedata.post.diyfields?pagedata.post.diyfields:[]
		for (var i=0;i<filedata.length;i++){
			if((filedata[i].ismust ==1 || filedata[i].ismust ==1) && !filedata[i].data){
				app.util.msg(filedata[i].title +'-不能为空！','error');
				return false
			}
			if(filedata[i].type ==8 && (filedata[i].ismust ==1 || filedata[i].ismust ==1)){
				if(!(str.test(filedata[i].data))){
				  app.util.msg("请输入正确的手机号码！",'error')
				   return false;
				}
			}
			if(filedata[i].type ==9 && (filedata[i].ismust ==1 || filedata[i].ismust ==1)){
				if(!(stremail.test(filedata[i].data))){
				  app.util.msg("请输入正确的邮箱！",'error')
				   return false;
				}
			}
		}
		app.util.post('jy_weishop/order.createall/post/',pagedata.post,function(res){
			page.setData({
				'pagedata.order_id':res.order_id
			});
			pagedata.order_id=res.order_id
			app.util.pay(res.pay_id,function(res){		
				if(res.type=='more'){
					page.setData({
						showpaymodal:true,
						'pagedata.paymodal':res
					});
				}else if(res.type=='success'){
					if(app.setting.apptype!='aliapp' && app.setting.apptype!='ttapp'){
						page.sendsubmsg(pagedata.order_id);
					}else{
						app.util.msg(res.message,'','jy_weishop/order.list/');
					}
				}else{
					app.util.msg(res.message);
				}
			});
		});
	}
});